package com.vlong.town.controller.admin.restaurant;
import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.vlong.town.controller.base.BaseController;
import com.vlong.town.entity.base.DataMessage;
import com.vlong.town.entity.restaurant.Restaurant;
import com.vlong.town.service.restaurant.RestaurantService;
import com.vlong.town.util.common.TownConstant;
import com.vlong.town.util.data.DataUtil;
import com.vlong.town.util.pagination.PageTag;
/**
 * @author 
 */
@Controller
@RequestMapping("/restaurant")
public class RestaurantController extends BaseController  {

	// Fields

	@Autowired
	private RestaurantService restaurantService;// 业务接口

	// Methods


    /**
	* 进入
	* @param pageNo 当前页码
	* @param pageSize 每页记录数
	* @return restaurant/add_restaurant
	*/
	@RequestMapping(value="/list")
	public String gotoRestaurantListPage(
			@RequestParam(required=false, value="pageNo") Integer pageNo,
			@RequestParam(required=false, value="pageSize") Integer pageSize,
			@RequestParam(required=false, value="restaurantName") String restaurantName
			) {
		 PageHelper.startPage(getPageNo(pageNo), getPageSize(pageSize));
	     PageHelper.orderBy("update_time DESC");
	     List<Restaurant> restaurantList = this.restaurantService.queryList(null, null, restaurantName, 0, TownConstant.TOWN_STATUS_2, null, null, null, null, null, null);
	     PageInfo<Restaurant> pageInfo = new PageInfo<Restaurant>(restaurantList);
         PageTag<Restaurant> pt = new PageTag<Restaurant>(request, "list", pageInfo);
         request.setAttribute("pageHtml", pt.getPageHtml());
         request.setAttribute("restaurantName", restaurantName);
         request.setAttribute("status", TownConstant.TOWN_STATUS_2);
         for(Restaurant restaurant : restaurantList) {
        	 restaurant.setContent(DataUtil.delHTMLTag(restaurant.getContent(), 0));
         }
         request.setAttribute("restaurantList", restaurantList);
	     return "admin/restaurant/list_restaurant";
	}
	
	/**
	* 进入
	* @param pageNo 当前页码
	* @param pageSize 每页记录数
	* @return restaurant/add_restaurant
	*/
	@RequestMapping(value="/listNo")
	public String gotoRestaurantListNoPage(
			@RequestParam(required=false, value="pageNo") Integer pageNo,
			@RequestParam(required=false, value="pageSize") Integer pageSize,
			@RequestParam(required=false, value="restaurantName") String restaurantName
			) {
		 PageHelper.startPage(getPageNo(pageNo), getPageSize(pageSize));
	     PageHelper.orderBy("update_time DESC");
	     List<Restaurant> list=new  ArrayList<Restaurant>();
	     List<Restaurant> restaurantList = this.restaurantService.queryList(null, null, restaurantName, 0, TownConstant.TOWN_STATUS_1, null, null, null, null,null, null);
	     List<Restaurant> restaurantList3 = this.restaurantService.queryList(null, null, restaurantName, 0, TownConstant.TOWN_STATUS_3, null, null, null, null,null, null);
	     list.addAll(restaurantList);
	     list.addAll(restaurantList3);
	     PageInfo<Restaurant> pageInfo = new PageInfo<Restaurant>(list);
         PageTag<Restaurant> pt = new PageTag<Restaurant>(request, "list", pageInfo);
         request.setAttribute("pageHtml", pt.getPageHtml());
         request.setAttribute("restaurantName", restaurantName);
         request.setAttribute("status", TownConstant.TOWN_STATUS_1);
         for(Restaurant restaurant : list) {
        	 restaurant.setContent(DataUtil.delHTMLTag(restaurant.getContent(), 0));
         }
         request.setAttribute("restaurantList", list);
	     return "admin/restaurant/list_restaurant";
	}


    /**
	* 进入
	* @return restaurant/add_restaurant
	*/
	@RequestMapping(value="/gotoAddPage", method=RequestMethod.GET)
	public String gotoRestaurantAddPage() {
		return "admin/restaurant/add_restaurant";
	}

   /**
	* 添加
	* 这里的参数需要根据实际情况自行修改
	* @return DataMessage->Json
	*/
	@ResponseBody
	@RequestMapping(value="/executeAddRestaurant")
	public DataMessage addRestaurant(
			@RequestParam(required=false, value="restaurantName") String restaurantName,
			@RequestParam(required=false, value="content") String content,
			@RequestParam(required=false, value="address") String address,
			@RequestParam(required=false, value="picture") String thumbnail
			) {
		DataMessage dm = new DataMessage();
		if(!validateStringProperty(restaurantName, 1, 20)){
			dm.setResult(TownConstant.RESULT_CODE_DATA_ERROR);
			dm.setMessage("饭店名称参数错误");
		}else{
			this.restaurantService.add(null, 1L, restaurantName, 0, TownConstant.TOWN_STATUS_2, content, address, thumbnail, null,System.currentTimeMillis(), null);
			dm.setResult(TownConstant.RESULT_CODE_SUCCESS);
			dm.setMessage("数据添加成功");
		}
		return dm;
	}

   /**
	* 进入
	* @param restaurantId 主键值
	* @return restaurant/edit_restaurant
	*/
	@GetMapping(value="/gotoEditPage/{restaurantId}")
	public String gotoRestaurantEditPage(@PathVariable(required=true, value="restaurantId") String restaurantId) {
		Restaurant restaurant = this.restaurantService.queryById(Long.parseLong(restaurantId));
		request.setAttribute("restaurant", restaurant);
		request.setAttribute("thumbnail",  request.getScheme()+"://"+request.getServerName()+":"+ request.getServerPort() + request.getContextPath() + "/cdkeditor/" + restaurant.getThumbnail());
		return "admin/restaurant/edit_restaurant";
	}

   /**
	* 编辑
	* 这里的参数需要根据实际情况自行修改
	* @return DataMessage->Json
	*/
	@ResponseBody
	@RequestMapping(value="/executeEditRestaurant", method=RequestMethod.POST)
	public DataMessage executeEditRestaurant(
			@RequestParam(required=false, value="restaurantId") String restaurantId,
			@RequestParam(required=false, value="restaurantName") String restaurantName,
			@RequestParam(required=false, value="content") String content,
			@RequestParam(required=false, value="address") String address,
			@RequestParam(required=false, value="picture") String thumbnail
			) {
		DataMessage dm = new DataMessage();
		if(!validateStringProperty(restaurantName, 1, 20)){
			dm.setResult(TownConstant.RESULT_CODE_DATA_ERROR);
			dm.setMessage("饭店名称参数错误");
		}else{
			this.restaurantService.edit(Long.parseLong(restaurantId), null, restaurantName, null, null, content, address, thumbnail,null, null, System.currentTimeMillis());
			dm.setResult(TownConstant.RESULT_CODE_SUCCESS);
			dm.setMessage("数据修改成功");
		}
		return dm;
	}

   /**
	* 删除
	* @param restaurantId 主键值
	* @return DataMessage->Json
	*/
	@ResponseBody
	@RequestMapping(value="/delete", method=RequestMethod.POST)
	public DataMessage deleteRestaurant(@RequestParam(required=true, value="restaurantId") Long restaurantId) {
		DataMessage dm = new DataMessage();
		if(restaurantId != null && restaurantId > 0L) {
			this.restaurantService.edit(restaurantId, null, null, 1, null, null, null, null,null, null, null);
			dm.setResult(TownConstant.RESULT_CODE_SUCCESS);
			dm.setMessage("数据删除成功");
		} else {
			dm.setResult(TownConstant.RESULT_CODE_PARAMETER_ERROR);
			dm.setMessage("参数错误");
		}
		return dm;
	}
	
  /**
	* 删除
	* @param restaurantId 主键值
	* @return DataMessage->Json
	*/
	@ResponseBody
	@RequestMapping(value="/batchDelete", method=RequestMethod.POST)
	public DataMessage batchDeleteRestaurant(@RequestParam(required=true, value="restaurantIds") String restaurantIds) {
		DataMessage dm = new DataMessage();
		if(restaurantIds!=null&&restaurantIds!=""){
			String[] split = restaurantIds.split(",");
			for (String restaurantId : split) {
				this.restaurantService.edit(Long.parseLong(restaurantId), null, null, 1, null, null, null, null,null, null, null);
			}
			dm.setResult(TownConstant.RESULT_CODE_SUCCESS);
			dm.setMessage("删除成功");
		}else {
			dm.setResult(TownConstant.RESULT_CODE_PARAMETER_ERROR);
			dm.setMessage("参数错误");
		}
		return dm;
	}
	
	/**
	* 通过
	* @param restaurantId 主键值
	* @return DataMessage->Json
	*/
	@ResponseBody
	@RequestMapping(value="/adoptRestaurant", method=RequestMethod.POST)
	public DataMessage adoptRestaurant(@RequestParam(required=true, value="restaurantId") Long restaurantId) {
		DataMessage dm = new DataMessage();
		if(restaurantId != null && restaurantId > 0L) {
			this.restaurantService.edit(restaurantId, null, null, null, TownConstant.TOWN_STATUS_2, null, null, null,null, null, null);
			dm.setResult(TownConstant.RESULT_CODE_SUCCESS);
			dm.setMessage("审核成功");
		} else {
			dm.setResult(TownConstant.RESULT_CODE_PARAMETER_ERROR);
			dm.setMessage("参数错误");
		}
		return dm;
	}

	/**
	 * 驳回
	 * @return
	 */
	@RequestMapping(value="/rejectRestaurant")
	public  String  rejectRestaurant(@RequestParam(required=true, value="restaurantId") Long restaurantId){
		request.setAttribute("restaurantId", restaurantId);
		return "admin/restaurant/reject_restaurant";
	}
	
	/**
	 * 更新驳回原因
	 * @param restaurantId
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="/updateReason", method=RequestMethod.POST)
	public DataMessage updateReason(
			@RequestParam(required=true, value="restaurantId") Long restaurantId,
			@RequestParam(required=true, value="reason") String reason
			) {
		DataMessage dm = new DataMessage();
		if(restaurantId != null && restaurantId > 0L) {
			this.restaurantService.edit(restaurantId, null, null, null, TownConstant.TOWN_STATUS_3, null, null, null,reason, null, null);
			dm.setResult(TownConstant.RESULT_CODE_SUCCESS);
			dm.setMessage("驳回成功");
		} else {
			dm.setResult(TownConstant.RESULT_CODE_PARAMETER_ERROR);
			dm.setMessage("参数错误");
		}
		return dm;
	}
	
	/**
	 * 查看
	 * @return
	 */
	@RequestMapping(value="/viewRestaurant")
	public String viewRestaurant(@RequestParam(required=true, value="restaurantId") Long restaurantId){
		Restaurant restaurant = this.restaurantService.queryById(restaurantId);
		request.setAttribute("restaurant", restaurant);
		return  "admin/restaurant/view_restaurant";
	}
	
	
	
	
	
}